{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 学员必读："
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "注释：# 后内容为注释，不会被python解释器所解释，不影响程序运行。\n",
    "<br>注释为了方便自己和他人理解程序，我们可以在程序代码中添加一些关于程序功能、算法、函数、数据的说明文字，从而加强程序代码的可读性。\n",
    "<br>对于代码开头的#，只需删除#点击运行即可，对于文字解说开头的#，无需进行任何操作"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "作业要求：\n",
    "<br>请同学们按照每一步骤的操作指引进行代码操作并逐行运行（跨行运行会导致报错呦）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "注意：如果同学在使用服务器完成作业时遇到困难，请在社群中向班班或者助教老师寻求帮助呦！\n",
    "<br>Jupyter服务器使用流程请参考以下文档：\n",
    "<br>https://doc.weixin.qq.com/doc/w3_AF4AjAaeAAkU80GMBtLR7CS4Hm5uq?scode=AI4A8gcnABAFNLrTb0AF4AjAaeAAk"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 一.读取数据"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "操作指引：删除#点击运行，即可运行代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#import pandas as pd \n",
    "#data = pd.read_csv('dataset.csv',encoding='ISO-8859-1') \n",
    "#data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 二.数据清洗"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.确定数据范围，找到业务数据符合业务规则 \n",
    "<br>2.清洗数据"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.根据业务需要提取数据,发货日期早于下单日期 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.1 转换字段【ShipDate】的时间类型\n",
    "<br>操作指引：删除#点击运行，即可运行代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#data['ShipDate'] = pd.to_datetime(data['ShipDate'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "根据以上操作转换字段【OrderDate】的时间类型\n",
    "<br>操作指引：复制上行代码，将代码中的ShipDate全部替换为OrderDate，点击运行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#请输入代码\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.2 计算【ShipDate】和【OrderDate】之间的时间差\n",
    "<br>操作指引：复制下行代码至“请输入代码”框中\n",
    "<br>删除#，将第一个方括号的星号内容改为ShipDate，将第二个方括号的星号内容改为OrderDate，点击运行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#data['interval'] = (data['***']-data['***']).dt.total_seconds()\n",
    "#data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#请输入代码\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.3 时间差，剔除不符合业务常识的数据，即ShipDate早于OrderDate\n",
    "<br>操作指引：复制下行代码至“请输入代码”框中\n",
    "<br>删除#，将方括号的星号内容改为0，点击运行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# data.drop(index=data[data.interval<***].index,inplace=True) \n",
    "#data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#请输入代码\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.找出售价为负的数据"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "操作指引：复制下行代码至“请输入代码”框中\n",
    "<br>删除#，将方括号的星号内容改为<0，点击运行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#售价为负 \n",
    "#data[data.Sales***]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#请输入代码\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.查看数据 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "操作指引：删除#点击运行，即可运行代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#data.info() "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4.数据清洗"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "脏数据：空值\\异常值\\重复值 \n",
    "<br>手段：弥补(字符串:众数;数字类型:平均值 ) , 删除(drop(col=[]))  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4.1 重复值 \n",
    "<br>unique() 不重复 \n",
    "<br>操作指引：删除#点击运行，即可运行代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#data.RowID.unique().size\n",
    "#data[data.RowID.duplicated()]\n",
    "#data.drop(index=data[data.RowID.duplicated()].index,inplace=True)\n",
    "#data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4.2 清洗ShipMode \n",
    "<br>空值, 数字类型 , 字符串(众数)\n",
    "<br>操作指引：删除#点击运行，即可运行代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#data[data.ShipMode.isnull()]\n",
    "#data.ShipMode.mode() #查看众数 \n",
    "#data['ShipMode'].fillna(value=data.ShipMode.mode()[0],inplace=True)\n",
    "#data\n",
    "#data[data.ShipMode.isnull()]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4.3 折扣数据\n",
    "<br>操作指引：删除#点击运行，即可运行代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#data[data.Discount>1] \n",
    "#data[data.Discount<0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "异常数据, 数字类型 \n",
    "<br>异常数据--->空值--->弥补 \n",
    "<br>操作指引：删除#点击运行，即可运行代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#data['Discount'] = data['Discount'].mask(data['Discount']>1, None)\n",
    "#data[data.Discount.isnull()]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "平均值 \n",
    "<br>操作指引：删除#点击运行，即可运行代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#df.Discount.平均  所有的值平均 \n",
    "#meanDiscount 把非空的平均 \n",
    "#meanDiscount = round(data[data.Discount.notnull()].Discount.sum()/data[data.Discount.notnull()].Discount.size,2)\n",
    "#data['Discount'].fillna(value=meanDiscount, inplace=True)\n",
    "#data\n",
    "#data.info()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4.4 删除postalCode \n",
    "<br>参考函数drop(col=[])\n",
    "<br>操作指引：在“请输输入代码”行中输入\n",
    "<br>data.drop(columns=['PostalCode'],inplace=True)\n",
    "<br>data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#请输入代码\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5.数据整理"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "清洗: 数据分析使用到相应的数据,评估 (数字类型字段)和 数据重要性\n",
    "<br>整理: 分析维度 , 整理\n",
    "<br>操作指引：删除#点击运行，即可运行代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#data['Order-year'] = data['OrderDate'].dt.year \n",
    "#data['Order-month'] = data['OrderDate'].dt.month\n",
    "#data['quarter'] = data['OrderDate'].dt.to_period('Q')\n",
    "#data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "同学可太棒啦，赶紧找班班要作业解析吧，看看自己的作业是不是100%正确呦！"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
